var form = layui.form,id=getQueryVariable("id"),code=getQueryVariable("code");

//自定义表单验证
form.verify({
    validateBankCard: function(value, item){ //value：表单的值、item：表单的DOM对象
        if(!/^([1-9]{1})(\d{14}|\d{18})$/.test(value) && value!= ""){
            return '请输入正确的银行卡账号';
        }
    }
});
//表单初始值，修改时调用
if(id!=""){
    var oldData =  parent.table.cache["banktableid"];
    $.each(oldData, function (index, value) {
        if (oldData[index]["id"] == id) {
            form.val("newbank-form",{
                "bankName":oldData[index]["bankName"],
                "bankAccount":oldData[index]["bankAccount"],
                "name":oldData[index]["name"],
                "remark":oldData[index]["remark"],
            })
        }
    });
    form.render();
}
//监听提交
form.on('submit(newbankinfo-form-element)', function(data){
    //data.field 为表单修改或新增数据 变量ID有值的情况下是修改，否则为新增
    var tf=false;
    if(id == ""){
        tf=savebank(data);
    }else{
        tf=updatebank(data);
    }
    if(tf){
        layer.msg("保存成功", {icon: 6});
        refrashbank(data);
    }else{
        layer.msg("保存失败", {icon: 6});
    }


    return false;
});
function  refrashbank(data) {
   setTimeout(function () {
      /*  var oldData =  parent.table.cache["banktableid"];
        if(id==""){
            data.field.id = oldData.length+1;
            oldData.push(data.field);
        }else {
            $.each(oldData, function (index, value) {
                if (oldData[index]["id"] == id) {
                    oldData[index]["bankName"] = data.field.bankName;
                    oldData[index]["bankAccount"] = data.field.bankAccount;
                    oldData[index]["name"] = data.field.name;
                    oldData[index]["remark"] = data.field.remark;
                }
            });
        }*/
        parent.table.reload('banktableid',{
            type:"get"
           ,url:"customer2bank/selectByPage"
           ,where:{
           "code":code
           }
            ,   page: {
                curr: 1 //重新从第 1 页开始
            }
        }); //刷新银行信息表格*/
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    },1000)
}
function  savebank(data) {

    data.field.code=code;
var tf=false;

    $.ajax({
        type : 'POST',
        //  url : 'resource/getTree',
        data:JSON.stringify(data.field),
        url:'customer2bank/insert',
        dataType:"json",
        contentType: 'application/json',
        async:false,
        success : function (da) {
            if(da.status==200 ){
              /*  var objs=da.data.split(":");
                customercode=objs[0];
                id=objs[0].id*/
                tf=true;
            }
        },
        error: function(data) {
            // layer.msg("获取失败!");
        }
    });

return tf;
}
function updatebank (data) {
    data.field.code=code;
    data.field.id=id;
    var tf=false;

    $.ajax({
        type : 'POST',
        //  url : 'resource/getTree',
        data:JSON.stringify(data.field),
        url:'customer2bank/update',
        dataType:"json",
        contentType: 'application/json',
        async:false,
        success : function (da) {
            if(da.status==200 ){
                /*  var objs=da.data.split(":");
                  customercode=objs[0];
                  id=objs[0].id*/
                tf=true;
            }
        },
        error: function(data) {
            // layer.msg("获取失败!");
        }
    });

    return tf;
}
